package cn.lcfms.app.admin.controller;



import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.lcfms.app.admin.bean.DepartBean;
import cn.lcfms.bin.annotation.PermitPoll;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.bin.view.CommonEditLayer;
import cn.lcfms.bin.view.CommonTreeView;




@PermitPoll(type="系统功能",name="部门管理")
@Controller("admin.DepartController")
@RequestMapping("/admin/depart")
public class DepartController extends AdminBaseController {
    /*
     * 添加部门
     */
	@RequestMapping("/add")
	public CommonEditLayer add(@RequestParam(defaultValue="0",required=false)int pid){
		CommonEditLayer layer=new CommonEditLayer();
		layer.setInputForm("部门名称", "dname", "", "unempty");
		layer.setViewForm("frame/departAdd.jsp");
		layer.addObject("pid",pid);	
		layer.setFormAction("saveDepart");
		return layer;
	}
	
	/*
     * 修改部门
     */
	@RequestMapping("/update")
	public CommonEditLayer update(int did){
		CommonEditLayer layer=new CommonEditLayer();
		BaseService service=Application.getService("department");
		DepartBean bean=service.selectOne(DepartBean.class,did);
		layer.setHiddenForm("did", bean.getDid());
		layer.setInputForm("部门名称", "dname", bean.getDname(), "unempty");
		layer.setViewForm("frame/departAdd.jsp");
		layer.addObject("pid",bean.getPid());	
		layer.setFormAction("saveDepart");
		return layer;
	}
	
	/*
     * 保存部门
     */
	@RequestMapping("/saveDepart")
	public String saveDepart(DepartBean bean){
		BaseService service=Application.getService("department");
		if(bean.getDid()==0) {
			service.insert(bean);
			return "{code:1,msg:添加成功！,url:list}";
		}else {
			service.update(bean);
			return "{code:1,msg:修改成功！,url:list}";
		}		
	}
	
	/*
     * 删除部门
     */
	@RequestMapping("/delete")
	public String delete(int did){
		BaseService service=Application.getService("department");
		service.deleteById(did);
		return "{code:1,msg:删除成功！,url:list}";
	}
		
	@RequestMapping(value="/getDepartNameById",produces="text/html;charset=utf-8")
	@ResponseBody
	public String getDepartNameById(int did){
		BaseService service=Application.getService("department");
		Object dname = service.where("did", did).selectColumn("dname");
		if(null!=dname) {
			return (String)dname;
		}
		return "不存在该部门";
	}
	/*
     * 部门列表
     */
	@RequestMapping("/list")
	public CommonTreeView list(){
		BaseService service=Application.getService("department");
		service.column("did as id","dname as 部门名称","pid");
		CommonTreeView view=new CommonTreeView("部门列表",0,service);	
		view.setId("id");
		view.setEditLayer("添加子部门", "icon-jia", "add?pid=${id}", 450, 250);
		view.setUpdateLayer("update?did=${id}", 450, 250);
		view.setDeleteLayer("delete?did=${id}");
		view.setToolBarLayer("添加部门", "icon-jia", "add", 450, 250);
		return view;
	}
}
